*******************************
 
 log using "PSRM replication_4_26.smcl", replace

 use "psrm4_22.dta", clear

**Figure 2**
  reg approval q_grow_pc c.q_gini_net q_log    
xtline approval if e(sample), tlabel(, angle(forty_five)) byopts(legend(off)) scheme(sj) byopts(graphregion(fcolor(white))) byopts(cols(3)) subtitle(, fcolor(gs14)) byopts(title(Figure 2. Quarterly Estimates of Presidential Approval in Latin America)) ytitle("") 

**Table 1*
**Model I***
 xtgls approval  l(0/1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change  if  rightpres==0   , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "table1 m1"
  
   **get number of admins in sample**
	 tab leader_change if e(sample)
   
 **Model III**  
xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0).q_log l(0/2).leader_change f(1).leader_change if  rightpres==0   , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "table1 m3"
  
   **get number of admins in sample**
	 tab leader_change if e(sample)
 
gen pipe = "|"

gen where=-3
 margins , dydx(l.q_grow_pc) at( q_gini_net=(.36(.001).54)) level(95)
	 

marginsplot, recast(line) recastci(rline)  yline(0) scheme(sj)  xtitle(Inequality) xscale(range(.35 .53)) xlabel(.35(.06).53) xtitle(, size(medsmall))  graphregion(fcolor(white) lcolor(white)) title("Leftist Presidents", size(medium)) xsize(5.84) ysize(4.25) addplot(scatter where q_gini_net if e(sample), plotr(m(b 4)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off) xlabel(.35(.06).53) below ) 

graph save Graph "left_growth", replace

drop where
gen where=-400
 quietly: margins , dydx(q_gini_net) at( l.q_grow_pc=(-10(1)16) ) level(95)
marginsplot, recast(line) recastci(rline)  yline(0) scheme(sj)  xtitle(Growth) xtitle(, size(medsmall))    graphregion(fcolor(white) lcolor(white)) title("Conditional Effect of Inequality on Approval for Leftist Presidents, 95% c.i.", size(medium)) xsize(5.84) ysize(4.25) addplot(scatter where q_grow_pc if e(sample), plotr(m(b 4)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off) below ) 
graph save Graph "left_inequality", replace

 **Model II**
  xtgls approval  l(0/1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==1   , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "table1 m2"
  
   **get number of admins in sample**
	 tab leader_change if e(sample)

 **Modle IV** 
 xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0).q_log l(0/2).leader_change  f(1).leader_change if  rightpres==1 , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "table1 m4", excel
  
   **get number of admins in sample**
	 tab leader_change if e(sample)

drop where
gen where=-3
  quietly: margins , dydx(l.q_grow_pc) at( q_gini_net=(.36(.001).54) ) level(95)
	 

marginsplot, recast(line) recastci(rline)  yline(0) scheme(sj)  xtitle(Inequality) xtitle(, size(medsmall))    graphregion(fcolor(white) lcolor(white)) title("Rightist Presidents", size(medium)) xsize(5.84) ysize(4.25)  addplot(scatter where q_gini_net if e(sample), plotr(m(b 4)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off) xlabel(.36(.06).54) below ) 
graph save Graph "right_growth", replace


drop where
gen where=-400
 quietly: margins , dydx(q_gini_net) at( l.q_grow_pc=(-14(1)13) ) level(95)
marginsplot, recast(line) recastci(rline)  yline(0) scheme(sj)  xtitle(Growth) xtitle(, size(medsmall))  graphregion(fcolor(white) lcolor(white)) title("Conditional Effect of Inequality on Approval for Rightist Presidents, 95% c.i.", size(medium)) xsize(5.84) ysize(4.25) addplot(scatter where q_grow_pc if e(sample), plotr(m(b 4)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off) xlabel(-14(5)13)below ) 
graph save Graph "right_inequality", replace
 
 ***Figure 3***
 graph combine "left_growth.gph" "right_growth.gph" , title("Figure 3. Conditional Effect of Growth on Approval, 95% c.i.", size(medium)) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
 graph save Graph "figure 3", replace
 
 
 ***table 2***
 xtgls approval  l(0/1).c.q_grow_pc c.q_gini_net l(0/1).q_log l(0/2).leader_change    f(1/2).leader_change  if rightpres~=. , cor(psar1) force    panels(heteroskedastic) rho(freg)
     outreg2 using "table 2 m1", replace dec(3)
	 
	 **get number of admins in sample**
	 tab leader_change if e(sample)

 xtgls approval  l(0/1).c.q_grow_pc c.q_gini_net l(0/1).q_log l(0/2).leader_change    f(1/2).leader_change  if rightpres==0 , cor(psar1) force    panels(heteroskedastic) rho(freg)
     outreg2 using "table 2 m2", replace dec(3)

	  **get number of admins in sample**
	 tab leader_change if e(sample)
  
 xtgls approval  l(0/1).c.q_grow_pc c.q_gini_net l(0/1).q_log l(0/2).leader_change    f(1/2).leader_change  if rightpres==1 , cor(psar1) force    panels(heteroskedastic) rho(freg)
     outreg2 using "table 2 m3", replace dec(3)
	 
	  **get number of admins in sample**
	 tab leader_change if e(sample)


 *****appendix*****
 
**Descriptive Stats**
**Table A1***
  reg approval q_grow_pc c.q_gini_net q_log    
sum approval  q_grow_pc c.q_gini_net rightpres q_log leader_change  if e(sample)

**Table A2***
by country_code: summarize qtr if e(sample), format

 
 **Table A3**
 **DR excluded from left analyses because of only 1 case**
 **DR included in main analyses, above, but as this shows, the exclusion does not change results substantively**
 
 
 xtpcse approval l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==0 & DR_code==.  ,  cor(psar1) het   
  outreg2 using "Table A3",  dec(3) replace

 xtpcse approval l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==1   ,  cor(psar1) het   
  outreg2 using "Table A3", dec(3)  append


 xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==0 & country~="Peru" & DR_code==.   , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "Table A3", dec(3) append

 xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==1 & country~="Peru"  , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "Table A3", dec(3) append

 xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==0 & country~="Venezuela" & DR_code==.   , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "Table A3", dec(3) append

 xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==1 & country~="Venezuela"  , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "Table A3", dec(3) append
 
 xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==0 & country~="Colombia" & DR_code==.   , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "Table A3", dec(3) append

 xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==1 & country~="Colombia"  , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "Table A3", dec(3) append

 xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==0 & country~="Colombia" & DR_code==.   , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "Table A3", dec(3) append

 xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change if  rightpres==1 & country~="Colombia"  , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "Table A3", excel dec(3) append
  
  **Table A4, fixed effects and 3-way interaction**
       xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change i.country_code if  rightpres==0   , cor(psar1) force    panels(i) rho(freg)
  outreg2 using "Table A4 m1",  replace
  
  xtgls approval  l(1).c.q_grow_pc##c.q_gini_net l(0/1).q_log l(0/2).leader_change f(1/2).leader_change i.country_code if  rightpres==1   , cor(psar1) force    panels(heteroskedastic) rho(freg)
  outreg2 using "Table A4 m2",  replace

  xtgls approval  l(1).c.q_grow_pc##c.q_gini_net##rightpres l(0/1).q_log l(0/2).leader_change f(1/2).leader_change    , cor(psar1) force    panels(heterosk) rho(freg)
    outreg2 using "Table A4 m3",  replace
	
		 quietly: margins , dydx(l.q_grow_pc) at( q_gini_net=(.30(.01).6) rightpres=(0 1) ) level(95) plot (recast(line) recastci(rline)  yline(0) scheme(sj)  xtitle(Growth) xtitle(, size(medsmall))  graphregion(fcolor(white) lcolor(white)) title("Figure A1. Conditional Effect of Growth - Based on 3-Way Interaction, 95% c.i.", size(medium)) xsize(5.84) ysize(4.25) by(rightpres))
marginsplot, recast(line) recastci(rline)  yline(0) scheme(sj)  xtitle(Growth) xtitle(, size(medsmall))  graphregion(fcolor(white) lcolor(white)) title("Figure A1. Conditional Effect of Growth - Based on 3-Way Interaction, 95% c.i.", size(medium)) xsize(5.84) ysize(4.25) by(rightpres)


  ***Memo & Appendix---inequality terciles***
   xtgls approval  l(1).c.q_grow_pc##left_gini_dummies l(0/1).q_log l(0/2).leader_change f(1/2).leader_change  if  rightpres==0   , cor(psar1) force    panels(heteroskedastic) rho(freg)
margins, dydx(l.q_grow_pc) at(left_gini_dummies=(0 1 2)) level(90)
marginsplot, recast(scatter) yline(0) scheme(sj) graphregion(fcolor(white) lcolor(white)) title("Leftist President") 
 graph save Graph "Figure A2 1", replace

 xtgls approval  l(1).c.q_grow_pc##right_gini_dummies l(0/1).q_log l(0/2).leader_change f(1/2).leader_change  if  rightpres==1   , cor(psar1) force    panels(heteroskedastic) rho(freg)
margins, dydx(l.q_grow_pc) at(right_gini_dummies=(0 1 2)) level(90)
marginsplot, recast(scatter) yline(0) scheme(sj) graphregion(fcolor(white) lcolor(white)) title("Rightist President")
 graph save Graph "Figure A2 2", replace


 graph combine "Figure A2 1" "Figure A2 2" , title("Figure A2. Conditional Effect of Growth on Approval by Inequality Tercile , 90% c.i.", size(medium)) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 

  ***end macro approval section***
  
  ***Individual Level Analyses section***
  
use "PRSM latinobarometer data.dta", clear


** generating table 3

** Model 1-no interaction

mixed ego_retro3 C.wt_grow C.gini_rec C.quintall_rec female age edu congruence || _all: R.year || country: quintall congruence   

** Model 2-interaction of inequality and growth

mixed ego_retro3 C.wt_grow##C.gini_rec C.quintall_rec female age edu congruence || _all: R.year || country: quintall congruence   

** Model 5-Three way interaction term

mixed ego_retro3 C.wt_grow##C.gini_rec##C.quintall_rec female age edu congruence || _all: R.year || country: quintall congruence   

** model 3-interaction of inequality and growth for the poorest quintile

mixed ego_retro3 C.wt_grow##C.gini_rec female age edu congruence || _all: R.year || country: congruence    if  quintall==1

** model 4-interaction of inequality and growth for the wealthiest quintile

mixed ego_retro3 C.wt_grow##C.gini_rec female age edu congruence || _all: R.year || country: congruence    if quintall==5

** Creating Figure 4

** run the model without GINI recentered

mixed ego_retro3 C.wt_grow##C.gini##C.quintall female age edu congruence || _all: R.year || country: quintall congruence  

gen pipe = "|"
gen where=0.0035
egen tag_gini = tag(gini)

** to generate graphs with the rugplot, you need to generate them a figure at a time

** do the poorest quintile

margins , dydx(wt_grow) at(gini=(.35(.02).55) quintall=(1)) level(95)
marginsplot, recast(line) recastci(rline) yline(0) addplot(scatter where gini if tag_gini, plotr(m(b 4)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off) below ) xtitle( "Inequality") ytitle("Estimated Marginal Effect") title("Lowest Quintile") xlabel(0.35(0.02)0.55)

** then edit the graph to take away the gridlines because nogrid does not seem to work and to make the margin white because fcolor does not work either

** Save the graph 
graph save Graph "Egotropic via Growth and Inequality-lowest quintall.gph", replace

** Repeat for the highest quintile 

margins , dydx(wt_grow) at(gini=(.35(.02).55) quintall=(5)) level(95)
marginsplot, recast(line) recastci(rline) yline(0) addplot(scatter where gini if tag_gini, plotr(m(b 4)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off) below ) xtitle( "Inequality") ytitle("Estimated Marginal Effect") title("Highest Quintile") xlabel(0.35(0.02)0.55) 

** then edit the graph to take away the gridlines because nogrid does not seem to work with marginsplot and to make the margin white because fcolor does not work either

** Save the graph
graph save Graph "Egotropic via Growth and Inequality-highest quintall.gph", replace

**then combine the graphs into figure 4

gr combine "Egotropic via Growth and Inequality-lowest quintall.gph" "Egotropic via Growth and Inequality-highest quintall.gph", title(Figure 4. Marginal Effects of Growth Rate on Egotropic Perceptions with 95% c.i.)

** Then edit the graph so that the outer egion is white and to wrap and center the title

graph save Graph "figure 4.gph", replace

log close
